import React from "react";
import { connect } from "react-redux";
import { HashRouter, Route, Routes, Navigate } from "react-router-dom";
import Login from "../views/login/Login";
import Detail from "../views/news/Detail";
import News from "../views/news/News";
import NewsSandBox from "../views/sandBox/NewsSandBox";

function indexRouter(props: { Login: boolean }) {
  //   localStorage.setItem("user", "111");
  return (
    <HashRouter>
      <Routes>
        <Route path="/login" element={<Login />}></Route>
        <Route path="/news" element={<News />}></Route>
        <Route path="/detail/:id" element={<Detail />}></Route>
        {/* <Route path="/*" element={ localStorage.getItem("user") ? <NewsSandBox /> : <Login/> }></Route> */}
        {props.Login ? (
          <Route path="/*" element={<NewsSandBox />}></Route>
        ) : (
          <Route path="/*" element={<Navigate to="/login" replace={true} />} />
        )}
      </Routes>
    </HashRouter>
  );
}

const mapStateToProps = ({
  UserReducer: { Login },
}: {
  UserReducer: { Login: boolean };
}) => {
  return {
    Login,
  };
};

export default connect(mapStateToProps)(indexRouter);
